define(['moment'], function () {
    return ['$scope', '$rootScope', '$filter', '$stateParams', '$mdDialog', 'config', 'MMWService', '$calculater', '$mddProvider',
        function ($scope, $rootScope, $filter, $stateParams, $mdDialog, config, $MMWService, $calculater, $mddProvider) {
            $scope._event = {};
            dialog = {};
            $scope._fn = {};
            $scope.warehouseInfo = {
                //select_type: '3',
                plot_no: '', wo_no: '', item_no: '', plot_info_detail: []
            }
            $scope.init = function () { 
                // 20230303 modify by Alan for#: module_setting卡控
                $scope.PTC_ASC = config.cache.module_setting.PTC_ASC;
                $MMWService.PNIGList = {};
                $scope.type = '1';
                $scope.selectedList = [];
                $scope.list_filter = {};
                $scope.filterSH = {};
                //20170612 modify by Dustdusk for 新增作廢功能
                //0:重工處理, 1:讓步處理, 2:誤判, 3:報廢入庫, 4:不良品
                //$scope.feedback_type = [0, 1, 2, 3, 4];
                if(!config.cache.permissions || config.cache.permissions.indexOf('B0225')!=-1){
                    $scope.isB0225 = true;
                }
            };

            $scope.back = function () {
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('home', true);
            };

            $scope._fn['search'] = {
                click: () => {
                    //先判斷是否有選取資料存在再決定是否詢問
                    if ($scope.selectedList.length == 0) {
                        search();
                    } else {
                        $scope.$root.showConfirm($filter('translate')('B0403.msg.keep_on_hold'), function (dialog) {
                            dialog.hide();
                            search();
                        });
                    }
                }
            }

            function search() {
                var content = {
                    wo_no: $scope.warehouseInfo.wo_no,
                    item_no: $scope.warehouseInfo.item_no,
                    type: '1',
                    do_flag: $scope.do_flag
                };
                warehousePlotInfoGet(content, function (response) {
                    $scope.list_filter = {};
                    $scope.filterSH = {};
                    $scope.selectedList = [];
                    if (response && response.length > 0) {
                        $scope.warehouseInfo.plot_info_detail = response;
                        $scope.warehouseInfo.plot_info_detail.forEach(function (detail) {
                            detail.checked = false;
                        });
                    } else {
                        $scope.warehouseInfo.plot_info_detail = [];
                        $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                    }
                });
            }

            $scope.ClickDoFlag = function(){
                if($scope.selectedList.length>0){
                    $scope.$root.showAlert($filter('translate')('B0403.msg.already_selected'));
                } else {
                    if($scope.do_flag == 1){
                        $scope.do_flag = 0;
                    } else {
                        $scope.do_flag = 1;
                    }
                    $scope.warehouseInfo.plot_info_detail = [];
                    $scope.selectedList = [];
                }
            };

            $scope._fn['confirm'] = {
                click: () => {
                    var content = {
                        rework_detail: [],
                        scrap_detail: []
                    };
                    $scope.selectedList.forEach(function (selectItem) {
                        var item = selectItem.extend_options;
                        // 20210422 modifyBy 14581 for M#89226 : 不良品判定增加傳遞線邊倉參數
                        // 20221206 modify by Alan for M#112097:  增加回傳ReasonType
                        // 20230321 modify by ShengKun for M# 0119704：不良品，成品序號檢查
                        if (selectItem.defect_type == '0' || selectItem.defect_type == '1') {
                            item.plot_detail = [{
                                plot_no: selectItem.plot_no,
                                plot_stamp: selectItem.plot_stamp,
                                op_no: selectItem.op_no,
                                status: selectItem.status,
                                qty: selectItem.defect_qty,
                                error_no: selectItem.error_no,
                                InventoryNo: item.inventoryno,
                                mes_inventoryNo: item.mes_inventoryNo,
                                ReasonType: item.ReasonType,
                                SNList: [{
                                    FGDINNO: "",
                                    LotNo: selectItem.plot_no,
                                    OPNo: selectItem.op_no,
                                    PCSNo: selectItem.pcs_no,
                                    PRODUCTNO: "",
                                    PRODUCTVERSION: ""
                                }]
                            }];
                            content.rework_detail.push(item);
                        } else {
                            item.plot_info_detail = [{
                                plot_no: selectItem.plot_no,
                                op_no: selectItem.op_no,
                                plot_qty: selectItem.defect_qty,
                                lot_serial: selectItem.lot_serial,
                                error_no: selectItem.error_no,
                                log_group_serial: selectItem.log_group_serial,
                                InventoryNo: item.inventoryno,
                                mes_inventoryNo: item.mes_inventoryNo,
                                ReasonType: item.ReasonType,
                                joint_item_no: selectItem.joint_trigger == undefined ? '' : selectItem.joint_item_no,
                                joint_item_unit_no: selectItem.joint_trigger == undefined ? '' : selectItem.joint_item_unit_no,
                                joint_trigger: selectItem.joint_trigger == undefined ? 'N' : 'Y',
                                SNList: [{
                                    FGDINNO: "",
                                    LotNo: selectItem.plot_no,
                                    OPNo: selectItem.op_no,
                                    PCSNo: selectItem.pcs_no,
                                    PRODUCTNO: "",
                                    PRODUCTVERSION: ""
                                }]
                            }];

                            content.scrap_detail.push(item);
                        }

                    });
                    warehousePlotProcess(content, function () {
                        $scope.warehouseInfo.plot_info_detail = [];
                        $scope.warehouseInfo.wo_no = '';
                        $scope.warehouseInfo.plot_no = '';
                        $scope.warehouseInfo.item_no = '';
                        $scope.selectedList = [];
                    });
                }
            }

            //20170405 modify by megabytes for 內存外顯修改
            //20170410 modify by megabytes for 改為皆顯示no
            //20170602 modify by Dustdusk for 開窗調整
            //20170602 modify by Dustdusk for 開窗取消
            //取得產品編號
            $scope._event['item_no'] = {
                click: () => {
                    //20170704 modify by Dustdusk for 入庫產品清單查詢優化
                    $MMWService.showQueryListPNIG({
                        //page_mode: 19,
                        form_name: 'PageNo_11',
                        pageRecord: 50,
                        key: 'page_no',
                        condition: [
                            { name: $filter('translate')('common.queryList.item_c'), column_name: 'B.ProductNo', query_mode: '2', value: $scope.warehouseInfo.item_no },
                            { name: $filter('translate')('common.queryList.item_n'), column_name: 'B.ProductName', query_mode: '2', value: '' }
                        ],
                        header: [
                            { label: $filter('translate')('common.queryList.item_c'), code: 'page_no' },
                            { label: $filter('translate')('common.queryList.item_n'), code: 'page_name' }
                        ],
                        selectCode: $scope.warehouseInfo.item_no,
                        confirm: function (selectItem) {
                            $scope.warehouseInfo.item_no = selectItem.page_no;
                            $scope.warehouseInfo.item_name = selectItem.page_display;
                            $scope.warehouseInfo.wo_no = '';
                        }
                    });
                }
            }
            //取得工單編號
            //20170602 modify by Dustdusk for 拿掉一定要輸入產品編號的限制
            //20170602 modify by Dustdusk for 開窗取消
            $scope._event['wo_no'] = {
                click: () => {
                    //20170704 modify by Dustdusk for 入庫工單清單查詢優化
                    $MMWService.showQueryListPNIG({
                        //page_mode: 21,
                        form_name: 'PageNo_15',
                        pageRecord: 50,
                        condition: [
                            { name: $filter('translate')('common.queryList.wo_c'), column_name: 'C.MoNo', query_mode: '2', value: $scope.warehouseInfo.wo_no }
                        ],
                        header: [
                            { label: $filter('translate')('common.queryList.wo_c'), code: 'page_no' }
                        ],
                        setCondition: function (conditions) {
                            if ($scope.warehouseInfo.item_no && $scope.warehouseInfo.item_no != '')
                                conditions.push({ column_name: 'B.ProductNo', query_mode: '0', value: $scope.warehouseInfo.item_no, merge_condition_model: 'AND' });

                            return conditions;
                        },
                        selectCode: $scope.warehouseInfo.wo_no,
                        confirm: function (selectItem) {
                            $scope.warehouseInfo.wo_no = selectItem.page_no;
                        }
                    });
                }
            }

            $scope._event['plot_no'] = {
                change: (plot_no, $event) => {
                    if ($event.event.keyCode == 13) {
                        $scope._fn['choose'].click();
                        $scope.$root.focusById('plot_no');
                    }
                }
            }
            $scope._event['selectedList.defect_qty'] = {
                click: (item, $event) => {
                    $scope.input_qty_check($event.event, $event.eventScope)
                }
            }

            $scope._fn['choose'] = {
                click: () => {
                    var targetArray = [];
                    for (var i = 0; i < $scope.warehouseInfo.plot_info_detail.length; i++) {
                        if ($scope.warehouseInfo.plot_info_detail[i].plot_no.indexOf($scope.warehouseInfo.plot_no) != -1) {
                            targetArray.push({
                                index: i,
                                item: $scope.warehouseInfo.plot_info_detail[i]
                            });
                        }
                    }
                    if (targetArray.length == 1) {
                        $scope.add(targetArray[0].index, targetArray[0].item);
                    } else if (targetArray.length == 0) {
                        $scope.$root.showAlert($filter('translate')('B0403.msg.scan_no_plot'));
                    } else {
                        $scope.$root.showAlert($filter('translate')('B0403.msg.scan_one_more_plot'));
                    }
                }
            }

            function addSelectItem(index, selectItem, defectItem){
                selectItem.qty = $calculater.decimalSub(selectItem.qty, defectItem.defect_qty);
                if(selectItem.qty == 0){
                    var _index = $scope.warehouseInfo.plot_info_detail.indexOf(selectItem);
                    $scope.warehouseInfo.plot_info_detail.splice(_index, 1);
                }
                $scope.selectedList.push(defectItem);
            }

            $scope.add = function (index, data) {
                if(data.ori_qty == undefined)
                    data.ori_qty = data.qty;
                if($scope.warehouseInfo.select_type == 0 || $scope.warehouseInfo.select_type == 1){
                    //重工處理(0)/讓步處理(1)
                    $mdDialog.serviceDialog('B0213Service',{
                        plot_detail : [angular.merge({}, data)],
                        option : $scope.warehouseInfo.select_type, //重工:0/讓步:1
                        parent : 'B0403',
                        confirm : function(content, dialog){
                            //流程、作業站、備註、重工原因
                            var defect_item = angular.merge({}, data);
                            defect_item.defect_type_display = $filter('translate')('B0403.defect_type.s_' + $scope.warehouseInfo.select_type);
                            defect_item.defect_type = $scope.warehouseInfo.select_type;
                            defect_item.defect_qty = content.plot_detail[0].qty;
                            defect_item.extend_options = content;
                            dialog.reworkInfo.reason_detail.forEach(function(item){
                                if(defect_item.defect_reason == undefined){
                                    defect_item.defect_reason = item.page_name;
                                } else {
                                    defect_item.defect_reason += ' / '+item.page_name;
                                }
                            });
                            delete defect_item.extend_options.plot_detail;
                            addSelectItem(index, data, defect_item);
                            dialog.hide();
                        }
                    });
                } else if($scope.warehouseInfo.select_type == 3 || $scope.warehouseInfo.select_type == 4){
                    //報廢(3) / 不良(4)
                    openStorage({
                        type:$scope.warehouseInfo.select_type,
                        plotItem:data,
                        confirm:function(dialog){
                            //ERP單別、庫別、儲位、不良原因列表
                            dialog.selectReasonList.forEach(function(badReasonItem){
                                var defect_item = angular.merge({}, data);
                                defect_item.defect_type_display = $filter('translate')('B0403.defect_type.s_' + $scope.warehouseInfo.select_type);
                                defect_item.defect_type = $scope.warehouseInfo.select_type;
                                defect_item.defect_reason_no = badReasonItem.page_no;
                                defect_item.defect_reason = badReasonItem.page_name;
                                defect_item.defect_qty = badReasonItem.qty;
                                defect_item.extend_options = {
                                    product_warehouse:dialog.warehouseInfo.product_warehouse,
                                    product_warehouse_name:dialog.warehouseInfo.product_warehouse_name,
                                    storage:dialog.warehouseInfo.storage,
                                    storage_display:dialog.warehouseInfo.storage_display,
                                    erp_doc_type:dialog.warehouseInfo.erp_doc_type,
                                    ncr_no:'',
                                    reason_no:badReasonItem.page_no
                                };
                                if($scope.warehouseInfo.select_type == '4'){
                                    defect_item.extend_options.type = '3';
                                } else if($scope.warehouseInfo.select_type == '3'){
                                    defect_item.extend_options.type = '1';
                                }
                                addSelectItem(index, data, defect_item);
                            });
                            dialog.hide();
                        }
                    });
                } else {
                    $calculater.open(function (num, dialog) {
                        var msg = '';
                        if(msg == ''){
                            // 20231226 modify by wenwei for M#153807: [H5][例外处理]不良现象判定-选择生产批，输入的数量可以超过生产批本身数量，显示负数
                            if(num > data.ori_qty) {
                                dialog.hide();
                                $scope.$root.showAlert($filter('translate')('B0403.overori_qty'))
                                return
                            }
                            var defect_item = angular.merge({}, data);
                            defect_item.defect_type_display = $filter('translate')('B0403.defect_type.s_' + $scope.warehouseInfo.select_type);
                            defect_item.defect_type = $scope.warehouseInfo.select_type;
                            defect_item.defect_qty = num;
                            defect_item.defect_reason_no = data.error_no;
                            defect_item.defect_reason = data.error_desc;
                            defect_item.extend_options = {
                                type:'2', product_warehouse:'', storage:'', erp_doc_type:'', ncr_no:''
                            };
                            addSelectItem(index, data, defect_item);
                            dialog.hide();
                        } else {
                            $scope.$root.showAlert(msg);
                        }
                    },
                    function (dialog) {dialog.hide();},
                    {
                        defaultNum: data.qty,
                        decimal: true
                    });
                }
            };

            $scope.selecttype = function (type) {
                $scope.warehouseInfo.select_type = type;
            };

            $scope.remove = function (index, data) {
                var selectItem;
                for(var i = 0 ; i < $scope.warehouseInfo.plot_info_detail.length ; i++){
                    if($scope.warehouseInfo.plot_info_detail[i].rowid == data.rowid){
                        selectItem = $scope.warehouseInfo.plot_info_detail[i];
                        break;
                    }
                }

                if(selectItem == undefined){
                    //表示舊的已經不在
                    data.qty = data.defect_qty;
                    delete data.extend_options;
                    delete data.defect_type;
                    delete data.defect_qty;
                    delete data.extend_options;
                    delete data.defect_reason;
                    $scope.warehouseInfo.plot_info_detail.push(data);
                }else{
                    //表示舊的還在
                    selectItem.qty = $calculater.decimalAdd(selectItem.qty, data.defect_qty);
                }
                $scope.selectedList.splice($scope.selectedList.indexOf($scope.selectedList[index]), 1);
            };

            function openStorage(options) {
                var content = {
                    //page_mode: 8,
                    form_name: 'PageNo_53',
                    query_condition_info: []
                };
                if($scope.warehouseInfo.select_type == 3){//報廢
                    content.query_condition_info.push({column_name: 'ReasonType', query_mode: 0, value: 7, merge_condition_model: 'AND'});
                } else if($scope.warehouseInfo.select_type == 4){//不良品
                    content.query_condition_info.push({column_name: 'ReasonType', query_mode: 0, value: 13, merge_condition_model: 'AND'});
                }
                $MMWService.pageNumberInfoGet(content, function(badcodeList){
                    $mddProvider.UseForm('B0403_factory', () =>{
                        $mdDialog.dialog('module/moduleB/program/B04/B0403/B0403.factory.tmp.html',
                            function (dialog) {
                                let _hide = dialog.hide;
                                return {
                                    type:options.type,
                                    plotItem:options.plotItem,
                                    warehouseInfo: {
                                        product_warehouse:'',//庫別
                                        storage:'',//儲位
                                        erp_doc_type:''//ERP單別
                                    },
                                    selectReasonList: [],
                                    _title: 'B0403.factory.title_' + options.type,
                                    _event: {
                                        'erp_doc_type': {
                                            click: () => {
                                                $rootScope.showQueryList({
                                                    pageRecord: 50,
                                                    header: [
                                                        { label: $filter('translate')('B0216.queryList.column3_1'), code: 'page_no' },
                                                        { label: $filter('translate')('B0216.queryList.column3_2'), code: 'page_name' }
                                                    ],
                                                    query: function (conditions, feedback) {
                                                        $MMWService.sendToServer({
                                                            uri: 'ServicesSTD.Module_SYS.erp_document_type_get',
                                                            content: { doc_type: ($scope.warehouseInfo.select_type == 4) ? '1' : '2' }, // 不良品入庫=入庫單，報廢入庫=報廢單
                                                            success: function (data) {
                                                                feedback(data);
                                                            }
                                                        });
                                                    },
                                                    selectCode: dialog.warehouseInfo.erp_doc_type,
                                                    confirm: function (selectItem, radioDialog) {
                                                        dialog.warehouseInfo.erp_doc_type = selectItem.page_no;
                                                        radioDialog.hide();
                                                    }
                                                });
                                            }
                                        },
                                        'product_warehouse_name': {
                                            click: () => {
                                                // 良品 & 不良品 = 1 , 報廢 = 3
                                                pageNumberInfoGet({
                                                    //page_mode: '13',
                                                    form_name: 'PageNo_6',
                                                    query_condition_info: [
                                                        { column_name: 'InventoryType', query_mode: '0', value: ($scope.warehouseInfo.select_type == 3) ? 3 : 1 }
                                                    ]
                                                }, function (response) {
                                                    $scope.$root.showSelect({
                                                        title: $filter('translate')('B0403.otherTitle.column1'),
                                                        label: function (item) {
                                                            return item.page_no + ' (' + item.page_name + ')';
                                                        },
                                                        code: 'page_no',
                                                        selectCode: $scope.warehouseInfo.product_warehouse,
                                                        list: response,
                                                        confirm: function (item, radioDialog) {
                                                            dialog.warehouseInfo.product_warehouse_name = item.page_name;
                                                            dialog.warehouseInfo.product_warehouse = item.page_no;

                                                            dialog.warehouseInfo.storage = '';
                                                            dialog.warehouseInfo.storage_display = '';

                                                            radioDialog.hide();
                                                        }
                                                    });
                                                });
                                            }
                                        },
                                        'storage': {
                                            click: () => {
                                                if (dialog.warehouseInfo.product_warehouse != '') {
                                                    pageNumberInfoGet({
                                                        form_name: 'PageNo_7',
                                                        query_condition_info: [
                                                            { column_name: 'InventoryNo', query_mode: '0', value: dialog.warehouseInfo.product_warehouse }
                                                        ]
                                                    }, function (response) {
                                                        $scope.$root.showSelect({
                                                            title: $filter('translate')('B0403.otherTitle.column1'),
                                                            label: function (item) {
                                                                return item.page_no + ' (' + item.page_name + ')';
                                                            },
                                                            code: 'page_no',
                                                            selectCode: dialog.warehouseInfo.storage,
                                                            list: response,
                                                            confirm: function (item, radioDialog) {
                                                                radioDialog.hide();
                                                                dialog.warehouseInfo.storage = item.page_no;
                                                                dialog.warehouseInfo.storage_display = item.page_name;
                                                            }
                                                        });
                                                    });
                                                } else {
                                                    $scope.$root.showAlert($filter('translate')('B0403.msg.product_warehouse_empty'));
                                                }
                                            }
                                        },
                                        'dialog.selectReasonList.reason_display': {
                                            displayText: $filter('translate')('B0403.factory.list.column1_' + options.type)
                                        }
                                    },
                                    _fn: {
                                        'Execute': {
                                            click: () => {
                                                dialog.loadBadReasonList();
                                            }
                                        },
                                        'confirm': {
                                            click: () => {
                                                var msg = '';
                                                if (dialog.warehouseInfo.product_warehouse == '') {
                                                    //請選擇庫別
                                                    //msg += $filter('translate')('B0403.msg.product_warehouse_empty')+'<br>';
                                                }
                                                if (dialog.selectReasonList.length == 0) {
                                                    //請輸入不良原因
                                                    msg += $filter('translate')('B0403.msg.bad_reason_list_empty');
                                                } else {
                                                    var defect_qty = 0;
                                                    dialog.selectReasonList.forEach(function (item) {
                                                        if (item.qty != '' && item.qty != 0) {
                                                            defect_qty = $calculater.decimalAdd(defect_qty, item.qty);
                                                        } else {
                                                            //請輸入不良數量
                                                            msg += $filter('translate')('B0403.msg.defect_qty_empty') + '<br>';
                                                        }
                                                    });
                                                    if ((+defect_qty) > (+dialog.plotItem.qty)) {
                                                        //不良數量總和不可大於生產數量
                                                        msg += $filter('translate')('B0403.msg.defect_qty_gt') + dialog.plotItem.qty;
                                                    }
                                                }
                                                if (msg == '') {
                                                    dialog.defect_qty = defect_qty;
                                                    options.confirm(dialog);
                                                } else {
                                                    $scope.$root.showAlert(msg);
                                                }
                                            }
                                        }
                                    },
                                    loadBadReasonList: function (item) {
                                        $rootScope.showQueryList({
                                            key: 'page_no',
                                            title: $filter('translate')('B0403.factory.queryList.reworkReason_select_' + dialog.type),
                                            header: [
                                                { label: $filter('translate')('common.queryList.reason_c'), code: 'page_no' },
                                                { label: $filter('translate')('common.queryList.reason_n'), code: 'page_name' }
                                            ],
                                            query: function (conditions, feedback) {
                                                feedback(badcodeList);
                                            },
                                            confirm: function (selectItem, radioDialog) {
                                                var temp = $filter('filter')(dialog.selectReasonList, { reason_no: selectItem.page_no });
                                                if (temp.length == 0) {
                                                    if (item != undefined) {
                                                        item.reason_no = selectItem.page_no;
                                                        item.page_no = selectItem.page_no;
                                                        item.page_name = selectItem.page_name;
                                                        item.reason_display = selectItem.page_no + ' ' + selectItem.page_name;
                                                    } else {
                                                        dialog.selectReasonList.push({
                                                            reason_display: selectItem.page_no + ' ' + selectItem.page_name,
                                                            reason_no: selectItem.page_no,
                                                            page_no: selectItem.page_no,
                                                            page_name: selectItem.page_name,
                                                            qty: ''
                                                        });
                                                    }
                                                    radioDialog.hide();
                                                } else {
                                                    $scope.$root.showAlert($filter('translate')('B0403.msg.bad_reason_repeat'));
                                                }
                                            }
                                        });
                                    },
                                    removeRow:function(index){
                                        dialog.selectReasonList.splice(index, 1);
                                    },
                                    hide: function () {
                                        _hide();
                                    }
                                };
                            }
                        );
                    })
                 });
            }

            //取得單號
            // 1 工單編號 6產品編號 13成品庫 14儲位
            function pageNumberInfoGet(content, feedback) {
                $MMWService.pageNumberInfoGet(
                    content
                    , function (itemList) {
                        feedback(itemList);
                });
            }

            //取得待入庫資料
            //type 0:良品  1:不良品
            function warehousePlotInfoGet(content, feedback) {
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_WIP.warehouse_plot_info_get',
                    content: content,
                    success: function (data) {
                        feedback(data.warehouse_plot_info_get);
                    }
                });
            }

            //送出確定的待入庫資料
            //type 0:良品 1:不良品
            function warehousePlotProcess(content, callback) {
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_WIP.rejects_process',
                    content: content,
                    success: function (data, response) {
                        //20180514 modify by Dustdusk for M#46940:入庫完成的訊息由後台提供 請抓 data.SysMsg
                        // 20230926 modify by wenwei for M#131125
                        if (data.reworkLotList && data.reworkLotList != ''){
                            var msg = $filter('translate')('B0403.msg.process_success_rework_lotno') + data.reworkLotList.join('、');
                        }else{
                            var msg = $filter('translate')('B0403.msg.process_success');
                        }
                        if(data.SysMsg != undefined && data.SysMsg!=''){
                            msg = data.SysMsg;
                        }
                        //20220606 modify by panghx for #110681:不良现象判定报废入库执行完成的信息由后台提供，抓取data.Message
                        if (response.Message != undefined && response.Message != '') {
                            msg = response.Message;
                        }
                        $scope.$root.showToast(msg);
                        if(callback)
                            callback();
                    }
                });
            }

            $scope.input_cancel_qty = function (event, item) {
                var target = event.currentTarget;
                angular.element(target).addClass('is-focus');
                $calculater.open(function (num, dialog) {
                        angular.element(target).removeClass('is-focus');
                        if(num == 0){
                            $scope.$root.showAlert($filter('translate')('B0403.msg.cancel_qty_empty'));
                        } else if ((+num) > (+item.qty)){
                            $scope.$root.showAlert($filter('translate')('B0403.msg.cancel_qty_gt'));
                        } else {
                            item.cancel_qty = num;
                            dialog.hide();
                        }
                    },
                    function (dialog) {
                        angular.element(target).removeClass('is-focus');
                        dialog.hide();
                    },
                    {
                        defaultNum: item.cancel_qty ,
                        decimal: false
                    });
            };
            // 20240325 add by wenwei For M#160479: [Oracle][H5][生产任务中心]例外处理-不良品判定/不良现象判定，修改判定数量的值大于不良数量，需提示判定数量不可大于不良数量
            $scope.input_qty_check = function (event, item) {
                var target = event.currentTarget;
                angular.element(target).addClass('is-focus');
                $calculater.open(function (num, dialog) {
                    angular.element(target).removeClass('is-focus');
                    if(num == 0){
                        $scope.$root.showAlert($filter('translate')('B0403.msg.qty_empty'));
                    } else if ((+num) > (+item.qty)){
                        $scope.$root.showAlert($filter('translate')('B0403.overori_qty'));
                    } else {
                        item.defect_qty = num;
                        dialog.hide();
                    }
                },
                function (dialog) {
                    angular.element(target).removeClass('is-focus');
                    dialog.hide();
                },
                {
                    defaultNum: item.defect_qty ,
                    decimal: false
                });
            };

            $scope.product_change = function(event, item){
                $mdDialog.serviceDialog('B0223Service',{
                    parent : 'B0403',
                    plotItem : item,
                    do_flag: 1,
                    close : function(newItem){
                        item.item_no = item.joint_item_no; //  20240221 modify by shiwl for #0157806: [sMES] 聯產品入庫時已選取好的品號再次點進選單後，點選關閉視窗，品號會跳回原生產批品號 [應用奈米]
                        item.item_name = newItem.item_name;
                        item.item_unit_no = newItem.item_unit_no;
                        item.item_ver = newItem.item_ver;
                    },
                    feedback : function(newItem){
                        item.old_item_no = item.item_no;
                        item.old_item_name = item.item_name;
                        item.old_item_unit_no = item.item_unit_no;
                        item.old_item_ver = item.item_ver;

                        item.joint_item_no = item.item_no = newItem.joint_item_no;
                        item.item_name = newItem.joint_item_name;
                        item.joint_item_unit_no = item.item_unit_no = newItem.joint_item_unit_no;
                        item.item_ver = newItem.joint_item_ver;
                        item.joint_trigger = 'Y';
                    }
                });
            };

            $scope.callB0225 = function(item){
                //如有分量則不得還原
                if(item.ori_qty != undefined && item.ori_qty != item.qty){
                    $scope.$root.showAlert($filter('translate')('B0403.msg.can_not_restore'));
                } else {
                    $mdDialog.serviceDialog('B0225Service',{
                        parent:'B0403',
                        plotItem : item,
                        callback : function(){
                            $scope._fn['search'].click();
                        }
                    });
                }
            };
        }
    ];
});
